package edu.xlh.travel.dao;

import edu.xlh.travel.pojo.Notice;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface NoticeDao extends JpaRepository<Notice,String>, JpaSpecificationExecutor<Notice> {
    List<Notice> findTOP6ByOrderByIssuedTimeDesc();

    @Modifying
    @Query(value = "update t_notice set " +
            "title= CASE WHEN  :#{#notice.title} IS NULL THEN title ELSE :#{#notice.title} END, " +
            "content= CASE WHEN  :#{#notice.content} IS NULL THEN content ELSE :#{#notice.content} END, " +
            "issued_time= CASE WHEN  :#{#notice.issuedTime} IS NULL THEN issued_time ELSE :#{#notice.issuedTime} END " +
            "where id =:#{#notice.id}",nativeQuery = true )
    void updateNoticeByNoticeId(@Param("notice") Notice notice);

    @Transactional
    @Modifying
    @Query(value = "update Notice set delFlag =?2 where id =?1")
    void updateNoticeDelFlagByNoticeId(String noticeId, String delFlag);
}
